package org.ldh.dao.impl;

import org.ldh.dao.UserDao;
import org.ldh.domain.User;
import org.ldh.util.ConnectionFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户的持久层实现类
 */
public class UserDaoImpl implements UserDao {

    @Override
    public User findByNameAndPass(String username, String password) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "select * from user where username = ? and password = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参数
            pstm.setString(1,username);
            pstm.setString(2,password);
            //执行sql
            rs = pstm.executeQuery();
            //指针默认指向表头，向下移动一行
            while(rs.next()){
                //从数据库中查询出数据，封装成实体类
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));

                return user;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
        return null;
    }

    @Override
    public List<User> findAll() {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "select * from user";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //执行sql
            rs = pstm.executeQuery();
            List<User> users = new ArrayList<User>();
            //指针默认指向表头，向下移动一行
            while(rs.next()){
                //从数据库中查询出数据，封装成实体类
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));

                users.add(user);
            }
            return users;
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
        return null;
    }

    @Override
    public void save(User user) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        try{
            //编写sql
            String sql = "insert into user(username,password) values(?,?)";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符设置参数
            pstm.setString(1,user.getUsername());
            pstm.setString(2,user.getPassword());
            //执行sql
            pstm.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }

    @Override
    public User findById(String id) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            //编写sql
            String sql = "select * from user where id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参数
            pstm.setInt(1, Integer.parseInt(id));
            //执行sql
            rs = pstm.executeQuery();
            //指针默认指向表头，向下移动一行
            while (rs.next()) {
                //从数据库中查询出数据，封装成实体类
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));

                return user;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
        return null;
    }

    @Override
    public void deleteById(String id) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        try {
            //编写sql
            String sql = "delete from user where id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参数
            pstm.setInt(1, Integer.parseInt(id));
            //执行sql
            pstm.executeUpdate();

        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }

    @Override
    public void update(User user) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        try {
            //编写sql
            String sql = "update user set username = ?,password = ? where id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参数
            pstm.setString(1,user.getUsername());
            pstm.setString(2,user.getPassword());
            pstm.setInt(3, user.getId());
            //执行sql
            pstm.executeUpdate();

        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }

    @Override
    public void updatePassByName(String username, String password) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        try {
            //编写sql
            String sql = "update user set password = ? where username = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参数
            pstm.setString(1,password);
            pstm.setString(2,username);
            //执行sql
            pstm.executeUpdate();

        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }
}
